* Import De Loecker/Eckhout/Unger markups    
* ============================================

import delimited ${dirInput}/DLE_markups_fig.txt, clear

gen aux_daily = dofy(year)
gen date_quarterly = qofd(aux_daily) + 3 // assumes that we observe end-of-year data
drop aux_daily
format date_quarterly %tq
tsset date_quarterly

tsfill

gen markup_agg_fill = markup_agg
replace markup_agg_fill = F.markup_agg_fill if markup_agg_fill == .
replace markup_agg_fill = F.markup_agg_fill if markup_agg_fill == .
replace markup_agg_fill = F.markup_agg_fill if markup_agg_fill == .

ipolate markup_agg date_quarterly, generate(markup_agg_linter)

save ${dirTemp}/DLE_markups, replace 

* Import Fernald (2014) TFP 
* ============================================  
import excel using ${dirInput}/quarterly_tfp.xlsx, firstrow sheet(quarterly) cellrange(A2) clear
  
gen date_quarterly = quarterly(date, "YQ")
keep date_quarterly dtfp dtfp_util dY alpha dk dhours dLQ dutil dtfp_I dtfp_C
format date_quarterly %tq
keep if !missing(date_quarterly)  

gen t = _n
tsset t

gen dhLQ = dh + dLQ
 
gen logTFP = .
replace logTFP = 1 in 1
replace logTFP = logTFP[_n-1] + dtfp/400 if t>1

gen logTFPI = .
replace logTFPI = 1 in 1
replace logTFPI = logTFPI[_n-1] + dtfp_I/400 if t>1

gen logTFPC = .
replace logTFPC = 1 in 1
replace logTFPC = logTFPC[_n-1] + dtfp_C/400 if t>1
 
gen logY = .
replace logY = 1 in 1
replace logY = logY[_n-1] + dY/400 if t>1

gen logLq = .
replace logLq = 1 in 1
replace logLq = logLq[_n-1] + (dhours + dLQ)/400 if t>1

gen logL = .
replace logL = 1 in 1
replace logL = logL[_n-1] + (dhours)/400 if t>1

gen logH = .
replace logH = 1 in 1
replace logH = logH[_n-1] + dhours/400 if t>1

gen logK = .
replace logK = 1 in 1
replace logK = logK[_n-1] + dk/400 if t>1
 
sum alpha if date_q > yq(1995,1) 
gen logTFPalphaFix = logY - `r(mean)' * logK - (1-`r(mean)') * logH + 1

gen logUtil = .
replace logUtil = 1 in 1
replace logUtil = logUtil[_n-1] + dutil/400 if t>1

gen logTFPUtil = .
replace logTFPUtil = 1 in 1
replace logTFPUtil = logTFPUtil[_n-1] + dtfp_util/400 if t>1

gen logTFPIUtil = .
replace logTFPIUtil = 1 in 1
replace logTFPIUtil = logTFPIUtil[_n-1] + (dtfp_I - dutil)/400 if t>1

gen logTFPCUtil = .
replace logTFPCUtil = 1 in 1
replace logTFPCUtil = logTFPCUtil[_n-1] + (dtfp_C - dutil)/400 if t>1

* Markup-corrected TFP with time-varying markups from De Loecker/Eckhout/Unger
merge 1:1 date_quarterly using ${dirTemp}/DLE_markups, nogen
gen dtfp_markupDLE = dY - alpha*(markup_agg_linter)*dk - (1-alpha*(markup_agg_linter))*(dhours + dLQ) 
gen dtfp_util_markupDLE = dtfp_markupDLE - dutil
gen logTFPMarkupDLE = .
gen logTFPUtilMarkupDLE = .
egen aux = min(t) if ~missing(markup_agg_linter)
egen aux2 = mean(aux)
replace logTFPMarkupDLE = logTFP if t==aux2	
replace logTFPUtilMarkupDLE = logTFPUtil if t==aux2	
replace logTFPMarkupDLE = logTFPMarkupDLE[_n-1] + dtfp_markupDLE/400 if t>aux2	
replace logTFPUtilMarkupDLE = logTFPUtilMarkupDLE[_n-1] + dtfp_util_markupDLE/400 if t>aux2	
drop aux*

* Housekeeping
tsset date_quar
drop t
keep date_q logTFP* logTFP*Util logY logH logL* logK logUtil
save ${dirTemp}/tfp, replace


* Compute measurement-error corrected TFP 
* ==============================================  

* Capital stock
tempfile dK
import excel using ${dirInput}/quarterly_tfp.xlsx, firstrow sheet(quarterly) cellrange(A2) clear  
gen date_quarterly = quarterly(date, "YQ")
keep date_quarterly dtfp dtfp_util dY alpha dk dhours dLQ dutil dtfp_I dtfp_C
format date_quarterly %tq
keep if !missing(date_quarterly)   
keep dk date_q alpha dutil
save `dK'

* Hours
tempfile dL
import excel using ${dirInput}/us_total_hrs_emp.xlsx, firstrow   clear  sheet(Sheet1)
gen aux = quarterly(date_q, "YQ")
drop date_q
rename aux date_quarterly
tsset date_q
gen aux = log(total_hours)
gen dL = D.aux
keep date_q dL
save `dL'

* Measurement-error corrected GDP
import excel using ${dirInput}/gdpplus.xlsx, firstrow   clear 
gen date_quarterly = yq(OBS_YEAR, OBS_QUARTER) 
tsset date_q 
gen dGDPplus = GDPPLUS_DATA
keep date_q dGDPplus

* Merging
merge 1:1 date_q using `dL'
drop if _merge==2
drop _merge
merge 1:1 date_q using `dK'
drop if _merge==2
drop _merge

* Corrected TFP
gen dTFPplus = dGDPplus - alpha*dk - (1-alpha)*dL
gen dTFPplusUtil = dGDPplus - alpha*dk - (1-alpha)*dL - dutil

gen t = _n

gen logTFPplusUtil = .
replace logTFPplusUtil = 1 in 1
replace logTFPplusUtil = logTFPplusUtil[_n-1] + dTFPplusUtil/400 if t>1

gen logTFPplus = .
replace logTFPplus = 1 in 1
replace logTFPplus = logTFPplus[_n-1] + dTFPplus/400 if t>1

* Housekeeping
keep logTFPplus logTFPplusUtil date_q
save ${dirTemp}/tfpplus, replace 
